package pro.cherish.knowledgeumldesignpattern.designPattern.com.cherish.itrator;

import java.util.ArrayList;
import java.util.List;

/**
 * 自定义的聚合类
 */
public class ConcreteMyAggregate {

    private List<Object> list=new ArrayList<Object>();

    public void addObject(Object obj){
        this.list.add(obj);
    }

    public void removeObject(Object o){
        this.list.remove(o);

    }
    public List<Object> getList(Object o){
       return this.list;
    }
    public void setList( List<Object> list){
      this.list=list;
    }

    public MyIterator createIterator(){
        return new ConcreteIterator();
    }

//    使用内部类定义迭代器，可以直接使用外部类的属性
    private class ConcreteIterator implements MyIterator{
//定义游标便于记录遍历时的位置
//    如果需要逆向遍历则只需要将cursor初始为list.size()-1
        private int cursor;
        @Override
        public void First() {
            cursor=0;
        }
        @Override
        public void next() {
            if(cursor<list.size()){
                cursor++;
            }
        }
        @Override
        public boolean hashNext() {
            if(cursor<list.size()){
                return true;
            }
            return false;
        }

        @Override
        public boolean isFirst() {
            return cursor==0?true:false;
        }

        @Override
        public boolean isLast() {
            return cursor==list.size()?true:false;
        }

        @Override
        public Object getCurrentObj() {
            return  list.get(cursor);
        }
    }
}
